package com.ck.controller.teacher;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.ck.domain.Banji;
import com.ck.domain.Student;
import com.ck.service.TeacherService;

public class AddNewStudentController extends SimpleFormController {
	
	private TeacherService teacherService;

	public void setTeacherService(TeacherService teacherService) {
		this.teacherService = teacherService;
	}
	
	//公共变量，用于表单回显
	private List<Banji> banjiList = null;
	
	@Override
	protected Map referenceData(HttpServletRequest request, Object command,
			Errors errors) throws Exception {
		Map model = new HashMap();
		banjiList = teacherService.gainBanjiList();
		model.put("banjiList", banjiList);
		return model;
	}
	
	protected ModelAndView onSubmit(HttpServletRequest request,
            HttpServletResponse response, Object command, BindException errors)
            throws Exception {
		
		Student student = (Student) command;
		Map model = errors.getModel();
		
		//后台校验
		boolean flag = true;
		String username = student.getUsername().trim();
		String truename = student.getTruename().trim();
		Integer banjiID = Integer.parseInt(request.getParameter("banji"));
		String email = student.getEmail().trim();

		if(username == null || "".equals(username)) {
			model.put("error_username", "请输入账号");
			flag = false;
		}
		
		if(username.length() > 16) {
			model.put("error_username", "账号长度：1-16个字符");
			flag = false;
		}
		
		if(truename == null || "".equals(truename)) {
			model.put("error_truename", "请输入真实姓名");
			flag = false;
		}
		
		if(truename.length() > 12) {
			model.put("error_truename", "真实姓名长度：1-12个字符");
			flag = false;
		}
		
		if( banjiID == 0){
			model.put("error_banjiID", "请选择班级");
			flag = false;
		}
		
		if( email == null || "".equals(email) ){
			student.setEmail(null);
        }else{
	        Pattern p = Pattern.compile("\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
			Matcher m = p.matcher(email);
			if(!m.matches()){
				model.put("error_email", "email格式不正确");
				flag = false;
			}
        }
		
		if(email.length() > 40){
			model.put("error_email", "email长度：0-40个字符");
			flag = false;
		}
		
		//校验不通过
        if(!flag){
        	model.put("username", username);
        	model.put("truename", truename);
        	model.put("banjiID", banjiID);
        	model.put("email", email);
        	model.put("banjiList", banjiList);
			return new ModelAndView(getFormView(), model);
		}
		
		boolean isNewStudent = teacherService.addNewStudent(student, model);
		
		if (isNewStudent) {
            return new ModelAndView(getSuccessView(),model);
        } else {
        	model.put("username", username);
        	model.put("truename", truename);
        	model.put("banjiID", banjiID);
        	model.put("email", email);
        	model.put("banjiList", banjiList);
            return new ModelAndView(getFormView(), model);
        }
	}
}
